package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsPaper;
import yx.model.BsStudent;
import yx.vo.DatagridVO;
import yx.vo.PaperVO;

@Service("paperService")
public class PaperServiceImpl implements PaperServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PaperServiceImpl.class);

	private BaseDaoInt<BsPaper> paperDao;

	private BaseDaoInt<BsStudent> studentDao;

	public BaseDaoInt<BsPaper> getPaperDao() {
		return paperDao;
	}

	@Autowired
	public void setPaperDao(BaseDaoInt<BsPaper> paperDao) {
		this.paperDao = paperDao;
	}

	public BaseDaoInt<BsStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoInt<BsStudent> studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public PaperVO save(PaperVO paper) {
		BsPaper t = new BsPaper();
		BeanUtils.copyProperties(paper, t);
		paperDao.save(t);
		BeanUtils.copyProperties(t, paper);
		BsStudent stu = studentDao.get(BsStudent.class, paper.getStudentId());
		paper.setStudentName(stu.getStudentName());
		paper.setStudentNo(stu.getStudentNo());
		return paper;
	}

	@Override
	public DatagridVO datagrid(PaperVO paper) {
		DatagridVO dg = new DatagridVO();
		String hql = " from BsPaper p, BsStudent s where s.id = p.studentId";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(paper, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(paper, hql);
		List l = paperDao.find(hql, params, paper.getPage(), paper.getRows());
		List<PaperVO> nl = new ArrayList<PaperVO>();
		changeModel(l, nl);
		dg.setTotal(paperDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List l, List<PaperVO> nl) {
		if (l != null && l.size() > 0) {
			for (int i = 0; i < l.size(); i++) {
				Object[] obj = (Object[]) l.get(i);
				BsPaper p = (BsPaper) obj[0];
				BsStudent s = (BsStudent) obj[1];

				PaperVO vo = new PaperVO();
				BeanUtils.copyProperties(p, vo);
				vo.setStudentId(s.getId());
				vo.setStudentName(s.getStudentName());
				vo.setStudentNo(s.getStudentNo());
				nl.add(vo);
			}
		}
	}

	private String addOrder(PaperVO paper, String hql) {
		if (paper.getSort() != null || paper.getOrder() != null) {
			hql += " order by " + paper.getSort() + " " + paper.getOrder();
		}
		return hql;
	}

	private String addWhere(PaperVO paper, String hql, Map<String, Object> params) {
		if (paper.getStudentName() != null && !"".equals(paper.getStudentName().trim())) {
			hql += " and s.studentName like :studentName";
			params.put("studentName", "%%" + paper.getStudentName().trim() + "%%");
		}
		if (paper.getStudentNo() != null && !"".equals(paper.getStudentNo().trim())) {
			hql += " and s.studentNo = :studentNo";
			params.put("studentNo", paper.getStudentNo().trim());
		}
		if (paper.getPaperTitle() != null && !"".equals(paper.getPaperTitle().trim())) {
			hql += " and p.paperTitle like :paperTitle";
			params.put("paperTitle", "%%" + paper.getPaperTitle().trim() + "%%");
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsPaper t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		paperDao.executeHql(hql);
	}

	@Override
	public PaperVO edit(PaperVO paper) {
		BsPaper t = paperDao.get(BsPaper.class, paper.getId());
		BeanUtils.copyProperties(paper, t);
		return paper;
	}

}
